using AutoMapper;
using MediatR;
using Medical.Api.Write.Application.Command.RecyclingCleaning;
using Medical.Domain.Enum;
using Medical.Domain.RecoveryCleaning;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;

namespace Medical.Api.Write.Application.Handler.RecyclingCleaning
{
    /// <summary>
    /// 添加回收登记命令处理
    /// </summary>
    public class AddRecoveryRegistrationCommandHandlter : IRequestHandler<AddRecoveryRegistrationCommand, ApiResult<int>>
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<AddRecoveryRegistrationCommandHandlter> logger;
        /// <summary>
        /// automapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 回收登记仓储
        /// </summary>
        private readonly IBaseRepository<InstrumentRecovery> instrumentRecoveryRep;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mapper">automapper</param>
        /// <param name="instrumentRecoveryRep">回收登记仓储</param>
        public AddRecoveryRegistrationCommandHandlter(ILogger<AddRecoveryRegistrationCommandHandlter> logger, IMapper mapper, IBaseRepository<InstrumentRecovery> instrumentRecoveryRep)
        {
            this.logger = logger;
            this.mapper = mapper;
            this.instrumentRecoveryRep = instrumentRecoveryRep;
        }

        /// <summary>
        /// 添加回收登记命令处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns></returns>

        public async Task<ApiResult<int>> Handle(AddRecoveryRegistrationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                //回收登记映射
                var instrumentRecovery = mapper.Map<InstrumentRecovery>(request);
                instrumentRecovery.CreateTime = DateTime.Now;
                instrumentRecovery.Status = RecoveryStatusEnum.待回收;
                instrumentRecovery.RecoveredBy = request.RecoveredBy;
                instrumentRecovery.RecoveryTime = request.RecoveryTime;
                instrumentRecovery.ItemName = "";
                instrumentRecovery.UpdateUserName = "";
                instrumentRecovery.ApplyDepartment = "";
                instrumentRecovery.CreateUserName = "";
                instrumentRecovery.UpdateTime = DateTime.Now;
                instrumentRecovery.ItemCode = "";
                instrumentRecovery.ItemName = "";
                instrumentRecovery.Quantity = 0;
                instrumentRecovery.RecoveredQuantity = 0;
                instrumentRecovery.EquipmentPackageId = request.EquipmentPackageId;
                //添加回收登记信息
                var instrumen = instrumentRecoveryRep.Create(instrumentRecovery);

                if (instrumen > 0)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Success,
                        Data = instrumen,
                        Msg = "回收登记成功"
                    };

                }
                else
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Fail,
                        Data = instrumen,
                        Msg = "回收登记失败"
                    };
                }

            }
            catch (Exception ex)
            {
                logger.LogError("回收登记异常处理", ex.Message);
                throw;
            }


        }
    }
}
